home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / GopherTools / jughead / jughead.0.9 / getargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  12.2 KB  |  417 lines

  1. /*****************************************************************************
  2.  * File:    getargs.c
  3.  *
  4.  * Author:    Rhett "Jonzy" Jones
  5.  *        jonzy@cc.utah.edu
  6.  *
  7.  * Date:    May 20, 1993
  8.  *
  9.  * Modifed:    May 21, 1993, by Rhett "Jonzy" Jones.
  10.  *        Had to fix the -m flag.  The menus would not be displayed
  11.  *        with or without this flag set.
  12.  *
  13.  *        May 22, 1993, by Rhett "Jonzy" Jones.
  14.  *        Altered GetArguments() to work on a list of hosts and ports
  15.  *        or selection strings.
  16.  *
  17.  *        May 23, 1993, by Rhett "Jonzy" Jones.
  18.  *        Forgot to assign the correct value to 'initialHost', for use
  19.  *        with the -t flag.
  20.  *
  21.  * Description:    Gets the various arguments from the command line for use
  22.  *        with jughead.
  23.  *
  24.  * Routines:    static void    ShowArguments(void);
  25.  *        static short    UsageError(char *progName);
  26.  *        static short    PrintVersion(char *progName);
  27.  *        static short    GetParamsFromStdin(void);
  28.  *        static int    GetArg(char ***argv,char **argument);
  29.  *               int    GetArguments(int argc,char *argv[],
  30.  *                         char **fileName,char **logFile);
  31.  *
  32.  * Bugs:    No known bugs.
  33.  *
  34.  * Copyright:    Copyright 1993, University of Utah Computer Center.
  35.  *        This source may be freely distributed as long as this copyright
  36.  *         notice remains intact, and is in no way used for any monetary
  37.  *         gain, by any institution, business, person, or persons.
  38.  *
  39.  ****************************************************************************/
  40.  
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44.  
  45. #include "jughead.h"
  46.  
  47. extern List *CreateNode();    /* Defined in "jughead.c". */
  48.  
  49. /*****************************************************************************
  50.  * ShowArguments simply displays the status of the arguments passed in, and
  51.  * is solely for debugging.
  52.  ****************************************************************************/
  53. static void ShowArguments()
  54. {    int    i;    /* A loop counter. */
  55.     List    *t;    /* Pointer into the list of nogos. */
  56.  
  57.     fprintf(stderr,"flags are [%c%c%c%c%c%c%c%c%c%c%c%c%c%c]",
  58.                 printDTree     ? 'a' : '\0',
  59.                 printDTreeDirs     ? 'A' : '\0',
  60.                 buildIndex     ? 'B' : '\0',
  61.                 debug         ? 'd' : '\0',
  62.                 listHosts     ? 'h' : '\0',
  63.                 listHostsNPorts     ? 'H' : '\0',
  64.                 info4dirsOnly     ? 'i' : '\0',
  65.                 info4allItems     ? 'I' : '\0',
  66.                 menuFlag     ? 'm' : '\0',
  67.                 printLineNumbers ? 'n' : '\0',
  68.                 onlyDoHosts     ? 'o' : '\0',
  69.                 onlyDoHostsT     ? 'O' : '\0',
  70.                 doSearch     ? 'S' : '\0',
  71.                 time2process     ? 't' : '\0');
  72.     if (buildDataFile)
  73.         fprintf(stderr," -b [%s]",fileName);
  74.     if (selStr[0])
  75.         fprintf(stderr," -s [%s]",selStr);
  76.     if (strcmp(portStr,DEFAULTPORT) || searchPort != PORT2USE)
  77.         if (searchPort != PORT2USE)
  78.             fprintf(stderr," -p [%d]",searchPort);
  79.         else
  80.             fprintf(stderr," -p [%s]",portStr);
  81.     if (nogos)
  82.         for (t = nogos; t; t = t->next)
  83.             if (t->info.sStr[0])
  84.                 fprintf(stderr," -X [%s]",t->info.sStr);
  85.             else
  86.                 fprintf(stderr," -x [%s] [%s]",t->info.hStr,t->info.pStr);
  87.     if (logFile)
  88.         fprintf(stderr," -l [%s]",logFile);
  89.     if (buildIndex || doSearch)
  90.         fprintf(stderr," dataFile=[%s]",fileName);
  91.     if (numSearchHosts)
  92.         {
  93.         fprintf(stderr," hosts2search=(");
  94.         for (i = 0; i < numSearchHosts; i++)
  95.             fprintf(stderr," [%s]",searchHosts[i]);
  96.         fprintf(stderr," )");
  97.         }
  98.     if (userName)
  99.         fprintf(stderr," -u [%s]",userName);
  100.     if (!buildIndex && !doSearch && hostStr)
  101.         fprintf(stderr," host=[%s]",hostStr);
  102.     fprintf(stderr,"\n");
  103.  
  104. }    /* ShowArguments */
  105.  
  106. /*****************************************************************************
  107.  * UsageError prints the usage of the program and returns false so we can
  108.  * terminate.
  109.  ****************************************************************************/
  110. static short UsageError(progName)
  111.     char    *progName;    /* Name of the program. */
  112. {    int    numSpaces;    /* Number of spaces to print or the second
  113.                  * part of the main usage line. */
  114.  
  115.     numSpaces = strlen("usage: ") + strlen(progName) + 1;
  116.     fprintf(stderr,"usage: %s [-?aAdhHiImnoOtv] [-b dataFile] [-s selStr] [-p port#]\n",progName);
  117.     fprintf(stderr,"%*c[[-x hostNot2search port#]...] [[-X selStrNot2Search]...]\n",numSpaces,' ');
  118.     fprintf(stderr,"%*c[- | hosts2search...] host\n",numSpaces,' ');
  119.     fprintf(stderr,"usage: %s -B[?dtv] dataFile\n",progName);
  120.     fprintf(stderr,"usage: %s -S[?dtv] [-l log] [-p port#] [-u username] dataFile\n",progName);
  121.     return(0);
  122.  
  123. }    /* UsageError */
  124.  
  125. /*****************************************************************************
  126.  * PrintVersion prints the version of the program and returns false so we
  127.  * can terminate.
  128.  ****************************************************************************/
  129. static short PrintVersion(progName)
  130.     char    *progName;        /* The name of this program. */
  131. {
  132.     fprintf(stderr,"This version of %s is %s\n",progName,VERSION);
  133.     return(0);
  134.  
  135. }    /* PrintVersion */
  136.  
  137. /*****************************************************************************
  138.  * GetParamsFromStdin gets words from stdin until end-of-file is
  139.  * encounterd.  This routine also takes care of backspaces.
  140.  ****************************************************************************/
  141. static short GetParamsFromStdin()
  142. {    int    c,        /* A character from stdin. */
  143.         i;        /* A loop counter. */
  144.     short    done = 0;    /* Are we done yet? */
  145.     char    *s;        /* The string we are building. */
  146.  
  147.     if (isatty(STDIN_FILENO))
  148.         {
  149.         fprintf(stderr,"Please enter the hosts you would like to traverse\n");
  150.         fprintf(stderr,"separated by whitespace.  When you are finished\n");
  151.         fprintf(stderr,"enter ^D [control-d].\n  --> ");
  152.         }
  153.  
  154.     for (i = 0; (c = fgetc(stdin)) && !done && numSearchHosts < MAXHOSTS; i++)
  155.         if (isspace(buffer[i] = (char)c) || c == EOF)
  156.             {
  157.             if (c == EOF)
  158.                 done = 1;
  159.             buffer[i] = '\0';
  160.             if (i)
  161.                 if (s = (char *)malloc(i * sizeof(char)))
  162.                     searchHosts[numSearchHosts++] = StrToLower(strcpy(s,buffer));
  163.                 else
  164.                     {
  165.                     fprintf(stderr,"error: could not get memory for [%s]\n",buffer);
  166.                     exit(-2);
  167.                     }
  168.             i = -1;
  169.             }
  170.         else if (c == '\b')    /* Take care of backspace. */
  171.             {
  172.             i -= 2;
  173.             if (i < -1)
  174.                 i = -1;
  175.             }
  176.  
  177.     if (numSearchHosts >= MAXHOSTS)
  178.         fprintf(stderr,"warning: only the first %d hosts2search are being used.\n",MAXHOSTS);
  179.  
  180.     return(1);
  181.  
  182. }    /* GetParamsFromStdin */
  183.  
  184. /*****************************************************************************
  185.  * GetArg returns true if the current argument is exhausted, as in empty.
  186.  * Otherwise this routine returns false.  If returning true we assign the
  187.  * next argument in the list to 'argument', and adjust the list such that
  188.  * it is pointing to the second to last character in 'argument'.
  189.  ****************************************************************************/
  190. static int GetArg(argv,argument)
  191.     char    ***argv,        /* The list of arguments. */
  192.         **argument;        /* The argument to assign to. */
  193. {
  194.     if (!(*++**argv))        /* We're at the end of the list. */
  195.         {
  196.         *argument = *++*argv;    /* Get the next argument. */
  197.         while (***argv && *(**argv + 1))
  198.             ++**argv;
  199.         return(1);
  200.         }
  201.     else
  202.         return(0);
  203.  
  204. }    /* GetArg */
  205.  
  206. /*****************************************************************************
  207.  * GetArguments returns true if we were able to initialize variables from
  208.  * argc and argv.  Otherwise this routine calls UsageError() which exits, if
  209.  * something was wrong with the way the arguments were passed in.
  210.  ****************************************************************************/
  211. int GetArguments(argc,argv,fileName,logFile)
  212.     int    argc;
  213.     char    *argv[];
  214.     char    **fileName,    /* Name of the file to open or create. */
  215.         **logFile;    /* Name of the file to log to if searching. */
  216. {    char    *programName,    /* The name of the program. */
  217.         *s,        /* A temporary string. */
  218.         *exceptionHost,
  219.         *exceptionPort,
  220.         *thePort;    /* The port to use. */
  221.  
  222.     thePort = (char *)NIL;
  223.  
  224.     for (programName = *argv, argc--, argv++; *argv; argv++, argc--)
  225.         if (**argv == '-')
  226.             if (!*++*argv)
  227.                 GetParamsFromStdin();
  228.             else for ( ; **argv; ++*argv)
  229.                 switch(**argv)
  230.                     {
  231.                     case '?':
  232.                         return(UsageError(programName));
  233.                     case 'A':
  234.                         printDTree = printDTreeDirs = 1;
  235.                         break;
  236.                     case 'a':
  237.                         printDTree = 1;
  238.                         break;
  239.                     case 'B':
  240.                         buildIndex = 1;
  241.                         break;
  242.                     case 'b':
  243.                         buildDataFile = 1;
  244.                         if (argc > 1 && GetArg(&argv,fileName))
  245.                             argc--;
  246.                         else
  247.                             return(UsageError(programName));
  248.                         break;
  249.                     case 'd':
  250.                         debug = 1;
  251.                         break;
  252.                     case 'H':
  253.                         listHosts = listHostsNPorts = 1;
  254.                         break;
  255.                     case 'h':
  256.                         listHosts = 1;
  257.                         break;
  258.                     case 'I':
  259.                         info4allItems = 1;
  260.                         break;
  261.                     case 'i':
  262.                         info4dirsOnly = 1;
  263.                         break;
  264.                     case 'l':
  265.                         if (argc > 1 && GetArg(&argv,logFile))
  266.                             argc--;
  267.                         else
  268.                             return(UsageError(programName));
  269.                         break;
  270.                     case 'm':
  271.                         menuFlag = !menuFlag;
  272.                         break;
  273.                     case 'n':
  274.                         printLineNumbers = 1;
  275.                         break;
  276.                     case 'O':
  277.                         onlyDoHosts = onlyDoHostsT = 1;
  278.                         InitPath();
  279.                         break;
  280.                     case 'o':
  281.                         onlyDoHosts = 1;
  282.                         break;
  283.                     case 'p':
  284.                         if (argc > 1 && GetArg(&argv,&thePort))
  285.                             {
  286.                             argc--;
  287.                             for (s = thePort; *s; s++)    /* Validate the port. */
  288.                                 {
  289.                                 if (!isdigit(*s))
  290.                                     return(UsageError(programName));
  291.                                 }
  292.                             }
  293.                         else
  294.                             return(UsageError(programName));
  295.                         break;
  296.                     case 'S':
  297.                         doSearch = 1;
  298.                         break;
  299.                     case 's':
  300.                         if (argc > 1 && GetArg(&argv,&selStr))
  301.                             argc--;
  302.                         else
  303.                             return(UsageError(programName));
  304.                         break;
  305.                     case 't':
  306.                         time2process = 1;
  307.                         break;
  308.                     case 'v':
  309.                         return(PrintVersion(programName));
  310.                     case 'X':
  311.                         if (argc > 1 && GetArg(&argv,&s))
  312.                             {
  313.                             argc--;
  314.                             if (!nogos)
  315.                                 nogos = CreateNode(s,EMPTYSTRING,EMPTYSTRING);
  316.                             else
  317.                                 {
  318.                                 List *t = CreateNode(s,EMPTYSTRING,EMPTYSTRING);
  319.                                 t->next = nogos;
  320.                                 nogos = t;
  321.                                 }
  322.                             }
  323.                         else
  324.                             return(UsageError(programName));
  325.                         break;
  326.                     case 'x':
  327.                         if (argc > 2 && GetArg(&argv,&s))
  328.                             {
  329.                             argc -= 2;
  330.                             exceptionHost = StrToLower(s);
  331.                             if (GetArg(&argv,&exceptionPort))
  332.                                 for (s = exceptionPort; *s; s++)    /* Validate the port. */
  333.                                     {
  334.                                     if (!isdigit(*s))
  335.                                         return(UsageError(programName));
  336.                                     }
  337.                             else
  338.                                 return(UsageError(programName));
  339.                             if (!nogos)
  340.                                 nogos = CreateNode(EMPTYSTRING,exceptionHost,exceptionPort);
  341.                             else
  342.                                 {
  343.                                 List *t = CreateNode(EMPTYSTRING,exceptionHost,exceptionPort);
  344.                                 t->next = nogos;
  345.                                 nogos = t;
  346.                                 }
  347.                             }
  348.                         else
  349.                             return(UsageError(programName));
  350.                         break;
  351.                     case 'u':
  352.                         if (argc > 1 && GetArg(&argv,&userName))
  353.                             argc--;
  354.                         else
  355.                             return(UsageError(programName));
  356.                         break;
  357.                     default:
  358.                         return(UsageError(programName));
  359.                     }
  360.         else if (argc == 1)
  361.             if (buildIndex || doSearch)
  362.                 *fileName = *argv;
  363.             else
  364.                 initialHost = hostStr = *argv;
  365.         else if (numSearchHosts < MAXHOSTS)
  366.             searchHosts[numSearchHosts++] = StrToLower(*argv);
  367.         else
  368.             fprintf(stderr,"warning: Too many hosts to search.  Ignoring %s\n",*argv);
  369.  
  370.     if (buildDataFile)        /* Tweek the menu flag. */
  371.         menuFlag = !menuFlag;
  372.  
  373.     if (thePort)                /* User wants to use a different port. */
  374.         if (!doSearch && !buildIndex)
  375.             portStr = thePort;
  376.         else if (doSearch)
  377.             searchPort = atoi(thePort);
  378.         else if (buildIndex)
  379.             return(UsageError(programName));
  380.  
  381.     /* If no hosts to search just search the host. */
  382.     if (!numSearchHosts && !buildIndex && !doSearch)
  383.         searchHosts[numSearchHosts++] = hostStr;
  384.  
  385.     if (!*fileName && !hostStr)        /* Make sure we got the last argument. */
  386.         return(UsageError(programName));
  387.  
  388.     /* Make sure the proper flags have been used correctly:            *
  389.      * jughead [-?aAdhHiImnoOtv] [-b dataFile] [-s selStr] [-p port#]    *
  390.      *         [-x hostNot2search port#] [-X selStrNot2Search]        *
  391.      *         [- | hosts2search...] host                    *
  392.      * jughead -B[?dtv] dataFile                        *
  393.      * jughead -S[?dtv] [-l log] [-p port#] [-u username] dataFile        */
  394.     if ((buildIndex && doSearch) || (!doSearch && (*logFile || userName)) || (buildIndex && thePort))
  395.         return(UsageError(programName));
  396.     if ((buildIndex || doSearch) && (nogos ||
  397.                      selStr[0] ||
  398.                      printDTree ||
  399.                      printDTreeDirs ||
  400.                      listHosts ||
  401.                      listHostsNPorts ||
  402.                      info4allItems ||
  403.                      info4dirsOnly ||
  404.                      !menuFlag ||
  405.                      printLineNumbers ||
  406.                      onlyDoHosts ||
  407.                      onlyDoHostsT ||
  408.                      numSearchHosts))
  409.         return(UsageError(programName));
  410.  
  411.     if (debug)
  412.         ShowArguments();
  413.  
  414.     return(1);
  415.  
  416. }    /* GetArguments */
  417.